﻿using System.Collections.Generic;
using System.Web.UI;
using Microsoft.Practices.Unity;

namespace System.Web
{
    /// <summary>
    /// Unity容器扩展
    /// <para>2010/8/10</para>
    /// 	<para>TANGJINGBO</para>
    /// 	<para>tangjingbo</para>
    /// </summary>
    public static class UnityContainerExtensions
    {
        /// <summary>
        /// 
        /// <para>by tangjingbo at 2009-11-4 14:01</para>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="container">The container.</param>
        /// <param name="name">The name.</param>
        /// <param name="parmas">The parmas.</param>
        /// <returns></returns>
        public static T Resolve<T>(this IUnityContainer container, string name, IDictionary<string, object> parmas)
            where T : class
        {
            T excutor;
            if (string.IsNullOrEmpty(name))
                excutor = container.Resolve<T>();
            else
                excutor = container.Resolve<T>(name);
            foreach (var par in parmas)
            {
                excutor.GetType().GetProperty(par.Key).SetValue(excutor, par.Value, null);
            }
            return excutor;
        }

        /// <summary>
        /// 
        /// <para>by tangjingbo at 2009-11-4 14:01</para>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="container">The container.</param>
        /// <param name="parmas">The parmas.</param>
        /// <returns></returns>
        public static T Resolve<T>(this IUnityContainer container, IDictionary<string, object> parmas)
            where T : class
        {
            return Resolve<T>(container, string.Empty, parmas);
        }

        /// <summary>
        /// 
        /// <para>by tangjingbo at 2010/2/8 21:21</para>
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public static AopContext GetContext(this UserControl context)
        {
            return AopContext.Current;
        }

        /// <summary>
        /// 从AOP上下文中获取注册的服务对象
        /// <para>by tangjingbo at 2009-11-4 14:01</para>
        /// </summary>
        /// <typeparam name="TService">注册的类型</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="name">注册时的名字</param>
        /// <returns></returns>
        public static TService OfService<TService>(this HttpContext context, string name)
        {
            return AopContext.Default.Resolve<TService>(name);
        }

        /// <summary>
        ///  从AOP上下文中获取注册的服务对象
        /// <para>by tangjingbo at 2009-11-4 14:01</para>
        /// </summary>
        /// <typeparam name="TService">注册的类型.</typeparam>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public static TService OfService<TService>(this HttpContext context)
        {
            return AopContext.Default.Resolve<TService>();
        }

        #region HttpContext & HttpContextBase 转换扩展

        private const string contextbase_key = "contextbase_key";

        /// <summary>
        /// Contexts the base.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidCastException">类型转换异常</exception>
        public static HttpContextBase ContextBase(this HttpContext context)
        {
            var ctxbase = (HttpContextBase) context.Items[contextbase_key];
            if (ctxbase == null)
            {
                ctxbase = new HttpContextWrapper(context);
                context.Items[contextbase_key] = ctxbase;
            }
            return ctxbase;
        }

        #endregion
    }
}